package com.bee.portal.service.sm.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.bee.base.entity.Page;
import com.bee.portal.dao.sm.OrganizationDao;
import com.bee.portal.domain.sm.Organization;
import com.bee.portal.service.sm.OrganizationService;

public class OrganizationServiceImpl implements OrganizationService {

	private OrganizationDao organizationDao;
	private JdbcTemplate jdbcTemplate;

	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public Serializable save(Organization entity) {
		return organizationDao.save(entity);
	}

	@Override
	public void saveAll(Collection<Organization> entities) {
		organizationDao.saveAll(entities);
	}

	@Override
	public Organization get(Long sid) {
		return organizationDao.get(sid);
	}

	@Override
	public List<Organization> find(String queryString, Object... values) {
		return organizationDao.find(queryString, values);
	}

	@Override
	public List<Organization> find(Organization entity) {
		return organizationDao.find(entity);
	}

	@Override
	public List<Organization> find(Map<String, String> params) {
		return organizationDao.find(params);
	}

	@Override
	public List<Organization> findAll() {
		return organizationDao.findAll();
	}

	@Override
	public void update(Organization entity) {
		organizationDao.update(entity);
	}

	@Override
	public void updateAll(Collection<Organization> entities) {
		organizationDao.updateAll(entities);
	}

	@Override
	public void delete(Organization entity) {
		organizationDao.delete(entity);
	}

	@Override
	public void delete(Long sid) {
		organizationDao.delete(sid);
	}

	@Override
	public void deleteAll(Collection<Organization> entities) {
		organizationDao.deleteAll(entities);
	}

	@Override
	public Page page(String queryString, int pageNo, int pageSize) {
		return organizationDao.page(queryString, pageNo, pageSize);
	}

	@Override
	public Page page(Map<String, String> params, int pageNo, int pageSize) {
		return organizationDao.page(params, pageNo, pageSize);
	}

	@Override
	public List<Map<String, Object>> findOrg(Long parentSid) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append("SID as sid, CNAME as cname, IS_LEAF as isLeaf ");
		sql.append("FROM ");
		sql.append("SM_ORGANIZATION ");
		sql.append("WHERE ");
		sql.append("STATUS = 1 ");
		sql.append("AND ");
		sql.append("PARENT_SID = " + parentSid + " ");
		sql.append("ORDER BY SORT ASC ");
		return jdbcTemplate.queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> findOrgEx(Long parentSid, Boolean checked) {
		List<Map<String, Object>> result = this.findOrg(parentSid);
		for (Map<String, Object> map : result) {
			Long sid = Long.valueOf(map.get("sid").toString());
			if (checked) {
				map.put("checked", false);
			}
			map.put("expanded", true);
			map.put("children", this.findOrgEx(sid, checked));
		}
		return result;
	}

}
